const { dbMysql } = require('../../config/config');
const db = require('../../config/config')
module.exports = {
    // 登陆
    login(req, res) {
        let account = req.body.account
        let pwd = req.body.pwd
        let sql = `select * from guardian where relation = 0 and account=? and pwd=? and guardian_state=1`
        db.dbMysql(sql, [account, pwd], function (err, data) {
            if (data) {
                db.dbMysql(sql, [account, pwd], function (err, data) {
                    if (data) {
                        res.send({
                            code: 200,
                            data: data,
                            count: data.length,
                            msg: '查询成功'
                        })
                    }
                })
            }
        })
    },
    // 查看是否已存在
    check(req, res) {
        let account = req.body.account
        let sql = `select * from guardian where relation = 0 and account=? and guardian_state =1`
        db.dbMysql(sql, [account], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    count: data.length,
                    msg: '查询成功'
                })
            }
        })
    },
    // 注册
    register(req, res) {
        let account = req.body.account
        let pwd = req.body.pwd
        // let card = req.body.card
        // let nickName = req.body.nickName
        let sql = `insert into guardian(account,pwd) value (?,?);`
        db.dbMysql(sql, [account, pwd], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '添加成功'
                })
            }
        })
    },

    // 查询热门数据
    test(req, res) {
        let sql = `select * from task_all ORDER BY task_all_sales DESC LIMIT 6`
        db.dbMysql(sql, [], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }
        })
    },
    // 查询搜索查询
    serHistory(req, res) {
        let userId = req.body.userId
        let sql = ` select h.ser_id,h.ser_info,h.key_guardian_id,g.account,h.symbol from search_his as h  join guardian as g where h.key_guardian_id = g.guardian_id and h.key_guardian_id = ${userId} and h.ser_state = 1 order by h.symbol desc`
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                if (data) {
                    res.send({
                        code: 200,
                        data: data,
                        msg: '查询成功'
                    })
                }
            }


        })
    },
    // 删除搜索记录
    delHistory(req, res) {
        let userId = req.body.userId
        let symbol = req.body.symbol
        let sql = `update search_his set ser_state = 0 where key_guardian_id = ? and symbol = ?`
        db.dbMysql(sql, [userId, symbol], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    msg: '更改成功'
                })
            }
        })
    },
    // 添加搜索记录
    addHistory(req, res) {
        let ser_info = req.body.ser_info
        let key_guardian_id = req.body.key_guardian_id
        let symbol = req.body.symbol
        let sql = `insert into search_his (ser_info,key_guardian_id,symbol,ser_state) values (?,?,?,1)`
        db.dbMysql(sql, [ser_info, key_guardian_id, symbol], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    msg: '更改成功'
                })
            }
        })
    },
    // 删除全部
    delAll(req, res) {
        let userId = req.body.userId
        let sql = `update search_his set ser_state = 0 where key_guardian_id = ?`
        db.dbMysql(sql, [userId], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    msg: '更改成功'
                })
            }
        })
    },
    //热门搜索
    getHot(req, res) {
        let sql = `select * from task_all order by browsingCount desc limit 6`
        db.dbMysql(sql, [], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    msg: '更改成功'
                })
            }
        })
    },
    // 搜索结果数组
    getSearch(req, res) {
        let task_all_name = req.body.task_all_name
        let index = req.body.index
        sqlArr = [`SELECT * from task_all where task_all_name REGEXP '${task_all_name}' and task_all_state = 1`,
        `SELECT * from task_all where task_all_name REGEXP '${task_all_name}' and task_all_state = 1 order by task_all_sales desc`,
        `SELECT * from task_all where task_all_name REGEXP '${task_all_name}' and task_all_state = 1 order by task_all_price desc`,
        `SELECT * from task_all where task_all_name REGEXP '${task_all_name}' and task_all_state = 1 order by task_all_price`,]
        let sql = sqlArr[index]
        db.dbMysql(sql, [], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '查询成功'
                })
            }
        })

    },

    // --------------------任务分类页面请求
    // 任务类别，侧边栏
    getTaskType(req, res) {
        db.dbMysql(`SELECT * from task_type`, [], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '查询成功'
                })
            }
        })
    },
    // 对应任务类别的任务
    getTaskMsg(req, res) {
        let sql = ` SELECT * from task_all where key_task_type = ${req.body.key_task_type} and task_all_state = 1`
        db.dbMysql(sql, [], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '查询成功'
                })
            }
        })

    },
    // ---------购物车shopCart
    // 
    getShopArr(req, res) {
        let guardian_id = req.body.userId
        let sql = `select * from shop_cart,guardian,task_all where key_task_all_id = task_all_id and key_order_taker = guardian_id and key_order_taker = ?`
        db.dbMysql(sql, [guardian_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '添加成功'
                })
            }
        })
    },
    delShopTask(req, res) {
        let cart_id = req.body.cart_id
        let sql = `delete from shop_cart where cart_id=? `
        db.dbMysql(sql, [cart_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '删除成功'
                })
            }
        })
    },
    // 地址信息
    getAddress(req, res) {
        console.log("地址查询的擦混宋国开的", req.body);
        let userId = req.body.userId

        let sql = `select * from address as a left join guardian as g on a.key_guardian_id = g.guardian_id where a.key_guardian_id=${userId}`
        db.dbMysql(sql, [], function (err, data) {
            console.log("地址查询的sql", sql);
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '查询地址成功'
                })
            }
        })
    },
    // 新增地址
    addMyAddress(req, res) {
        let key_guardian_id = req.body.adUserId
        let harvester_name = req.body.adUserName
        let harvester_tel = req.body.adUserPhone
        let address_name = req.body.adName
        let sql = `INSERT INTO address(harvester_name,harvester_tel,address_name,key_guardian_id) VALUES (?,?,?,?)`
        db.dbMysql(sql, [harvester_name, harvester_tel, address_name, key_guardian_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '添加成功'
                })
            }
        })
    },
    // 编辑地址
    editMyAddress(req, res) {
        let harvester_name = req.body.form.adUserName
        let harvester_tel = req.body.form.adUserPhone
        let address_name = req.body.form.adName
        let address_id = req.body.editId
        let sql = `update address set harvester_name=?,harvester_tel=?,address_name=? where address_id = ?`
        db.dbMysql(sql, [harvester_name, harvester_tel, address_name, address_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '添加成功'
                })
            }
        })
    },
    // 删除地址
    delAdress(req, res) {
        let address_id = req.body.address_id
        let sql = `delete from address where address_id =?`
        db.dbMysql(sql, [address_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '删除地址成功'
                })
            }
        })
    },
    // 修改默认地址
    changeDefault(req, res) {
        let oldDefaultId = req.body.oldDefaultId
        let newDefaultId = req.body.newDefaultId
        let sql = `UPDATE address SET 
        isDefault = CASE address_id 
                WHEN ${oldDefaultId} THEN 0 
                WHEN ${newDefaultId} THEN 1 
        END
        WHERE address_id IN (${oldDefaultId},${newDefaultId});`
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '修改默认地址成功'
                })
            }
        })
    },
    // // 用户的服务者信息
    getServe(req, res) {
        let userId = req.body.userId
        let sql = `select * from guardian where guardian_id = ${userId} or key_guardian_id = ${userId}`
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    msg: '查询地址成功'
                })
            }
        })
    },
    // 加入购物车
    addShop(req, res) {
        let key_task_all_id = req.body.shopInfo.key_task_all_id
        let key_order_taker = req.body.shopInfo.key_order_taker
        let key_attendee = req.body.shopInfo.key_attendee
        let service_start_time = req.body.shopInfo.service_start_time
        let service_end_time = req.body.shopInfo.service_end_time
        let sql = `INSERT INTO shop_cart(key_task_all_id, key_order_taker, key_attendee,service_start_time,service_end_time) VALUES (?,?,?,?,?)`
        db.dbMysql(sql, [key_task_all_id, key_order_taker, key_attendee, service_start_time, service_end_time], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '添加成功'
                })
            }
        })
    },
    // 删除购物车
    delTaskInfo(req, res) {
        let arr = req.body
        let sql = `delete from shop_cart where cart_id in (${arr.join()})`
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '删除成功'
                })
            }
        })
    },
    // 付款加入
    addTaskInfo(req, res) {
        let arr = req.body
        let sql = `INSERT INTO task(key_task_all_id, key_order_taker, key_attendee,required_begin_time,required_end_time,create_time,task_remark,address_id) VALUES `
        let sqlStr = ''
        arr.forEach((item, index) => {
            let str = `(${item.key_task_all_id},${item.key_order_taker},${item.key_attendee},'${item.required_begin_time}','${item.required_end_time}','${item.create_time}','${item.task_remark}',${item.address_id})`
            if (index == arr.length - 1) {
                sqlStr += str
            } else {
                sqlStr += str + ','

            }
        });
        sql += sqlStr


        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '添加成功'
                })
            }
        })
    },

    // ---------个人中心
    // 全部订单--------展示没写
    getOrderArr(req, res) {
        console.log("订单请求的发送数据", req.body);
        let typeId = req.body.typeId
        let userId = req.body.userId
        let sql = ''
        if (typeId == 1) {
            sql = `SELECT task.*,g1.realName takerName,g2.realName servantName
            FROM task 
            INNER JOIN task_all ON task.key_task_all_id =task_all.task_all_id 
            INNER JOIN guardian as g1 ON task.key_order_taker = g1.guardian_id 
            INNER JOIN guardian as g2 ON task.key_attendee = g2.guardian_id 
            INNER JOIN address ON task.address_id = address.address_id 
            where task.order_plan = ? and task.key_order_taker=? and task.task_state=1;`
        } else {
            ` SELECT task.*,g1.realName takerName,g2.realName servantName
            FROM task 
            INNER JOIN task_all ON task.key_task_all_id =task_all.task_all_id 
            INNER JOIN guardian as g1 ON task.key_order_taker = g1.guardian_id 
            INNER JOIN guardian as g2 ON task.key_attendee = g2.guardian_id 
            INNER JOIN address ON task.address_id = address.address_id
            INNER JOIN escort ON task.key_servant = escort.escort_id
            where task.order_plan = ? and task.key_order_taker=? and task.task_state=1;`
        }

        db.dbMysql(sql, [userId, typeId], function (err, data) {
            console.log("订单请求的sql", sql);
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '添加成功'
                })
            }
        })
    },
    // 档案信息
    getUserINfoArr(req, res) {
        let userId = req.body.userId
        let sql = `select * from guardian where guardian_id = ${userId} or  key_guardian_id = ${userId} and guardian_state = 1`
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '查询成功'
                })
            }
        })
    },
    // 渲染修改列表页面
    editGetUser(req, res) {
        let userId = req.body.userId
        let sql = `select * from guardian where guardian_id = ${userId} `
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    count: data.length,
                    msg: '查询成功'
                })
            }
        })
    },
    editConfirm(req, res) {
        let imgUrl = req.body.userForm.imgUrl
        let nickName = req.body.userForm.nickName
        let realName = req.body.userForm.realName
        let phone = req.body.userForm.phone
        let location = req.body.userForm.location
        let age = req.body.userForm.age
        let card = req.body.userForm.card
        let gender = req.body.userForm.gender
        let guardian_id = req.body.userForm.guardian_id

        let sql = `update guardian set imgUrl=?,nickName=?,realName=?,phone=?,location=?,age=?,card=?,gender=? where guardian_id = ?`
        db.dbMysql(sql, [imgUrl, nickName, realName, phone, location, age, card, gender, guardian_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '查询成功'
                })
            }
        })
    },
    // 添加联系人
    addConfirm(req, res) {
        let imgUrl = req.body.userForm.imgUrl
        let nickName = req.body.userForm.nickName
        let realName = req.body.userForm.realName
        let phone = req.body.userForm.phone
        let location = req.body.userForm.location
        let age = req.body.userForm.age
        let card = req.body.userForm.card
        let gender = req.body.userForm.gender
        let relation = req.body.userForm.relation
        let key_guardian_id = req.body.userForm.key_guardian_id
        let sql = `insert into guardian(imgUrl,nickName,realName,phone,location,age,card,gender,relation,key_guardian_id) values (?,?,?,?,?,?,?,?,?,?)`
        db.dbMysql(sql, [imgUrl, nickName, realName, phone, location, age, card, gender, relation, key_guardian_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '添加联系人成功'
                })
            }
        })
    },
    // 删除联系人
    delRelation(req, res) {

        let guardian_id = req.body.guardian_id

        let sql = `update guardian set guardian_state=0 where guardian_id = ?`
        db.dbMysql(sql, [guardian_id], function (err, data) {
            console.log("删除联系人的sql", sql);
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '查询成功'
                })
            }
        })
    },

    uploadTest(req, res) {
        let id = req.body.userId
        let sql = `select * from upload where id = ${id}`
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    // count: data.length,
                    msg: '查询成功'
                })
            }
        })
    },
    // 档案信息
    getTaskDetails(req, res) {
        let task_all_id = req.body.taskId
        let sql = `select * from task_all a,task_type type where a.key_task_type = type.task_type_id and a.task_all_id = ?;`
        db.dbMysql(sql, [task_all_id], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }
        })
    },
    // 更改密码
    changePwd(req, res) {
        let {uerId,newPwd} = req.body
        let sql = `update guardian set pwd=? where guardian_id=?`
        db.dbMysql(sql, [newPwd,uerId], function (err, data) {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器报错 数据库报错'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '查询成功'
                })
            }
        })
    },
    // 查看商品详情


    // searchAdmin(req, res) {
    //     let kerWord = req.body.keyWord
    //     let sql = `select * from u_user where u_name like \'%${kerWord}%\' or u_id like \'%${kerWord}%\' or u_account like \'%${kerWord}%\' and u_isdel=0`

    //     db.dbMysql(sql, [kerWord], function (err, data) {
    //         if (data) {
    //             res.send({
    //                 code: 200,
    //                 data: data,
    //                 msg: '查询成功'
    //             })
    //         }
    //     })
    // },
    // yanZ(req, res) {
    //     let account = req.body.u_account
    //     db.dbMysql('select * from u_user where u_account=?',
    //         [account], function (err, data) {
    //             if (data.length == 0) {
    //                 res.send({
    //                     code: 200,
    //                     msg: "可用账号"
    //                 })
    //             } else {
    //                 res.send({
    //                     code: 201,
    //                     msg: '该账户已存在'
    //                 })
    //             }
    //         })
    // },
    // addAdmin(req, res) {
    //     let u_name = req.body.u_name;
    //     let u_account = req.body.u_account;
    //     let u_password = req.body.u_password;
    //     let sql = `insert into u_user(u_name,u_account,u_password) values (${u_name},${u_account},${u_password})`
    //     db.dbMysql(sql, [], function (err, data) {
    //         if (!err) {
    //             res.send({
    //                 code: 200,
    //                 data: data
    //             })
    //         } else {
    //             res.send({
    //                 code: 201,
    //                 err: err
    //             })
    //         }
    //     })
    // },

    // getAdmin(req, res) {
    //     db.dbMysql('select * from u_user where u_isdel=0',
    //         [], function (err, data) {
    //             if (!err) {
    //                 res.send({
    //                     code: 200,
    //                     data: data
    //                 })
    //             } else {
    //                 res.send({
    //                     code: 201,
    //                     err: err
    //                 })
    //             }
    //         })
    // },
    // getAdminOne(req, res) {
    //     db.dbMysql('select * from u_user where u_id=?',
    //         [req.body.u_id], function (err, data) {
    //             if (!err) {
    //                 res.send({
    //                     code: 200,
    //                     data: data
    //                 })
    //             } else {
    //                 res.send({
    //                     code: 201,
    //                     err: err
    //                 })
    //             }
    //         })
    // },
    // // 假删
    // delAdmin(req, res) {
    //     let u_id = req.body.u_id
    //     db.dbMysql('update u_user set u_isdel=1 where u_id=?', [u_id], function (err, data) {
    //         if (data) {
    //             res.send({
    //                 code: 200,
    //                 msg: '删除成功'
    //             })
    //         }
    //     })
    // },
    // // 修改
    // upDataAdmin(req, res) {
    //     let obj = req.body
    //     let u_id = obj.u_id;
    //     let u_name = obj.u_name;
    //     let u_password = obj.u_password;
    //     let u_type = obj.u_type;
    //     let u_account = obj.u_account;
    //     let u_isdel = obj.u_isdel
    //     db.dbMysql('update u_user set u_name=?,u_password=?,u_type=?,u_account=?, u_isdel=? where u_id=?', [u_name, u_password, u_type, u_account, u_isdel, u_id], function (err, data) {
    //         if (data) {
    //             res.send({
    //                 code: 200,
    //                 msg: '修改成功'
    //             })
    //         }
    //     })

    // }
}